//
//  ViewController.m
//  fireplace
//
//  Created by XiangDongMBP on 2017/2/24.
//  Copyright © 2017年 East. All rights reserved.
//

#import "ViewController.h"
#import "FIPTemperatureVC.h"
#import "FIPBulbVC.h"

@interface ViewController ()
@property (nonatomic, strong) FIPTemperatureVC *tempVC;
@property (nonatomic, strong) FIPBulbVC *bulbVC;
@property (nonatomic, strong) UIPageViewController *pageVC;
@property (weak, nonatomic) IBOutlet UIButton *temperatureBtn;
@property (weak, nonatomic) IBOutlet UIButton *bulbBtn;
@property (nonatomic, strong) NSArray<UIButton *> *btnArray;
@property (nonatomic, strong) NSArray<UIViewController *> *vcArray;
@end

@implementation ViewController

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNavigationBar];
    [self initSubViews];
}

- (void)initNavigationBar {
    
}

- (void)initSubViews {
//    self.pageVC.dataSource = self;
    [self.pageVC setViewControllers:@[self.tempVC] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    
    [self.temperatureBtn addTarget:self action:@selector(BtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.bulbBtn addTarget:self action:@selector(BtnTapped:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"segueIdEmbedPageVC"]) {
        self.pageVC = segue.destinationViewController;
    }
}

//#pragma mark - UIPageViewControllerDataSource
//- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
//    if (viewController == self.bulbVC) {
//        return self.tempVC;
//    }
//    return nil;
//}
//
//- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
//    if (viewController == self.tempVC) {
//        return self.bulbVC;
//    }
//    return nil;
//}

#pragma mark - event response
- (void)BtnTapped:(UIButton *)btn {
    NSInteger index = [self.btnArray indexOfObject:btn];
    [self.btnArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = (obj == btn);
    }];
    UIViewController *desVC = self.vcArray[index];
    [self.pageVC setViewControllers:@[desVC] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}

#pragma mark - private methods
#pragma mark - network request
#pragma mark - getters and setters
- (FIPTemperatureVC *)tempVC {
    if (!_tempVC) {
        _tempVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FIPTemperatureVC"];
    }
    return _tempVC;
}

- (FIPBulbVC *)bulbVC {
    if (!_bulbVC) {
        _bulbVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FIPBulbVC"];
    }
    return _bulbVC;
}

- (NSArray<UIButton *> *)btnArray {
    if (!_btnArray) {
        _btnArray = @[self.temperatureBtn, self.bulbBtn];
    }
    return _btnArray;
}

- (NSArray<UIViewController *> *)vcArray {
    if (!_vcArray) {
        _vcArray = @[self.tempVC, self.bulbVC];
    }
    return _vcArray;
}

@end
